<?php
$elementName = $this->element->getName();
$values      = $this->element->getXml();

//load values as url parameters
$module = null;
$paramsString = null;
$array = array();
if (isset($values) && !empty($values->$elementName) && '' != $values->$elementName) {
    // add controller_action
    $paramsString = (string)$values->$elementName;
    foreach ($values as $k => $v) {
        if ($k != $elementName) {
            $params[$k] = (string)$values->$k;
            $array[$k]  = $k . '/' . (string)$values->$k;
        }
    }
    // add all parameters as key/value pair
    $paramsString .= '/' . implode('/', $array);
    $module = $values->$elementName;
}

$attribs = $this->element->getAttribs();
if (!is_array($attribs)) {
    $attribs = null;
}
$select = $this->selectModule($elementName, $module, $attribs);
$select->setLabel($this->element->getLabel())
       ->setAttrib('class', $this->class);
if (isset($values->$elementName) && !empty($values->$elementName)) {
    $select->setValue($values->$elementName);
}
echo $select;?>

<div class="moduleFormWrapper" id="<?php echo $elementName;?>_wrapper"><?php
    if (isset($values->$elementName)) {
        $params['element']   = $elementName;
        $params['moduleKey'] = (string)$values->$elementName;
        echo $this->action('render-form', 'module', 'admin', $params);
    }?>
</div><?php

$uriString = '/admin/module/render-form/' . $paramsString . '/element/' . $elementName . '/moduleKey/';

// make sure this only loads once for each element
if (!Zend_Registry::isRegistered($elementName . 'Jquery')) {

    $this->jQuery()->onLoadCaptureStart();?>
        $('#<?php echo $select->getAttrib('id');?>').change(function(){
            var module = $(this).val();
            $('#<?php echo $elementName;?>_wrapper').load('<?php echo $uriString;?>' + module);
        });<?php
    $this->jQuery()->onLoadCaptureEnd();

    Zend_Registry::set($elementName . 'Jquery', true);
}
echo $this->formErrors($this->element->getMessages());